﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication5.Controller;
using WebApplication5.Model;

namespace WebApplication5.Pages
{
    public partial class Employee : System.Web.UI.Page
    {
        protected personCtr pCtr = new personCtr();
        protected void Page_Load(object sender, EventArgs e)
        {
            Site1 s = Page.Master as Site1;
            s.SelectedMenu = "Employee";

            
                List<WebApplication5.Model.Login> ha = pCtr.GetAllEmployee();
                var datasource = from h in ha
                                 select new
                                 {
                                     h.id,
                                     h.firstName,
                                     h.lastName,
                                     DisplayField = String.Format("{0} ({1})", h.firstName, h.lastName)
                                 };

                DropDownListEdit.DataSource = datasource;
                DropDownListEdit.DataValueField = "id";
                DropDownListEdit.DataTextField = "DisplayField";
                DropDownListEdit.DataBind();


                DropDownListDelete.DataSource = datasource;
                DropDownListDelete.DataValueField = "id";
                DropDownListDelete.DataTextField = "DisplayField";
                DropDownListDelete.DataBind();
            
        }

        protected void refresh()
        {
            List<WebApplication5.Model.Login> ha = pCtr.GetAllEmployee();
            var datasource = from h in ha
                             select new
                             {
                                 h.id,
                                 h.firstName,
                                 h.lastName,
                                 DisplayField = String.Format("{0} ({1})", h.firstName, h.lastName)
                             };

            DropDownListEdit.DataSource = datasource;
            DropDownListEdit.DataValueField = "id";
            DropDownListEdit.DataTextField = "DisplayField";
            DropDownListEdit.DataBind();


            DropDownListDelete.DataSource = datasource;
            DropDownListDelete.DataValueField = "id";
            DropDownListDelete.DataTextField = "DisplayField";
            DropDownListDelete.DataBind();
        }


        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                WebApplication5.Model.Login h = new WebApplication5.Model.Login();
                h.id = int.Parse(DropDownListEdit.SelectedValue);
                h.companyId = int.Parse(companyIdTbe.Text);
                h.firstName = firstnameTbe.Text;
                h.lastName = lastnameTbe.Text;
                h.password = passwordTbe.Text;
                h.phoneNumber = int.Parse(phoneNumberTbe.Text);
                h.userName = usernameTbe.Text;
                h.verfied = true;

                pCtr.editPerson(h);

                refresh();

            }


            catch (Exception exc)
            {

                string strm = exc.Message.Replace('\'', ' ');
                string str = "<script language='javascript'>";
                str += "alert('" + strm + "')";
                str += "</script>";
                ClientScript.RegisterStartupScript(typeof(Page), "showmessage", str);
            }
        }




        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            int id = int.Parse(DropDownListDelete.SelectedValue);

            pCtr.deletePerson(id);

            Shared.reloadCurrentTab(this);

            refresh();
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                WebApplication5.Model.Login person = new WebApplication5.Model.Login();

                person.companyId = int.Parse(companyTb.Text);
                person.password = passwordTb.Text;
                person.firstName = firstnameTb.Text;
                person.lastName = lastnameTb.Text;
                person.userName = usernameTb.Text;
                person.phoneNumber = int.Parse(phoneNumberTb.Text);
                person.Project = null;
                person.verfied = true;

                pCtr.addPerson(person);

                companyTb.Text = "";
                passwordTb.Text = "";
                firstnameTb.Text = "";
                lastnameTb.Text = "";
                usernameTb.Text = "";
                phoneNumberTb.Text = "";
                passwordTb.Text = "";

                
            }
                            catch (Exception exc)
            {
                string strm = exc.Message.Replace('\'', ' ');
                string str = "<script language='javascript'>";
                str += "alert('" + strm + "')";
                str += "</script>";
                ClientScript.RegisterStartupScript(typeof(Page), "showmessage", str);
            }

            refresh();
            }
    

        

        protected void ButtonGetEmployee_Click(object sender, EventArgs e)
        {


            int id = int.Parse(DropDownListEdit.SelectedValue);

            var H = pCtr.GetEmployee(id);

            companyIdTbe.Text = H.companyId.ToString();
            firstnameTbe.Text = H.firstName;
            lastnameTbe.Text = H.lastName;
            passwordTbe.Text = H.password;
            phoneNumberTbe.Text = H.phoneNumber.ToString();
            usernameTbe.Text = H.userName;
        }
    }
}